package com.example.paper.http.retrofit;

import com.example.paper.api.Api;
import com.example.paper.http.factory.HttpImpl;
import com.example.paper.interceptor.TokenInterceptor;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ToKenSingRetrofit implements HttpImpl {

    private static Retrofit retrofit;

    public ToKenSingRetrofit() {
    }

    @Override
    public Retrofit getRetrofit() {
        return retrofit;
    }

    public static class Build {
        public Build() {
            createRetrofit();
        }

        private void createRetrofit() {

            //拦截
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.addInterceptor(new TokenInterceptor());
            builder.addInterceptor(interceptor);
            builder.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS);
            builder.readTimeout(60 * 1000, TimeUnit.MILLISECONDS);
            builder.writeTimeout(60 * 1000, TimeUnit.MILLISECONDS);
            OkHttpClient build = builder.build();

            Retrofit.Builder builder1 = new Retrofit.Builder();
            Retrofit build1 = builder1
                    .baseUrl(BaseUrl)
                    .client(build)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();

            retrofit = build1;

        }

        //调用方法
        public ToKenSingRetrofit Build(){
            return new ToKenSingRetrofit();
        }

    }

}
